<template>
  <div>
    <el-divider>{{ title }}</el-divider>
    <draggable :list="columns" :animation="340" group="selectItem" handle=".option-drag">
      <div v-for="(item, index) in columns" :key="index" class="select-item">
        <div class="select-line-icon option-drag">
          <i class="el-icon-s-operation" />
        </div>
        <el-input v-model="item.label" :placeholder="title + '名'" size="small" />
        <el-input
          :placeholder="title + '值'"
          size="small"
          :value="item.value"
          @input="setOptionValue(item, $event)"
        />
        <div class="close-btn select-line-icon" @click="columns.splice(index, 1)">
          <i class="el-icon-remove-outline" />
        </div>
      </div>
    </draggable>
    <div style="margin-left: 20px">
      <el-button
        style="padding-bottom: 0"
        icon="el-icon-circle-plus-outline"
        type="text"
        @click="addSelectItem"
      >添加{{ title }}</el-button>
    </div>
  </div>
</template>

<script>
import draggable from "vuedraggable";

export default {
  components: {
    draggable
  },
  props: {
    options: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: "选项"
    }
  },
  data() {
    return {
      columns: this.options
    };
  },
  watch: {
    options(newValue, oldValue) {
      this.columns = newValue;
    }
  },

  methods: {
    addSelectItem() {
      this.columns.push({ label: "", value: "" });
      this.$emit("update:options", this.columns);
    },
    setOptionValue(item, val) {
      item.value = val + "";
    }
  }
};
</script>

<style lang="scss" scoped>
@import "../styles/generator.scss";

.select-item {
  display: flex;
  border: 1px dashed #fff;
  box-sizing: border-box;
  & .close-btn {
    cursor: pointer;
    color: #f56c6c;
  }
  & .el-input + .el-input {
    margin-left: 4px;
  }
}
.select-item + .select-item {
  margin-top: 4px;
}
.select-item.sortable-chosen {
  border: 1px dashed $themeColor;
}
.select-line-icon {
  line-height: 32px;
  font-size: 22px;
  padding: 0 4px;
  color: #777;
}
.option-drag {
  cursor: move;
}
</style>
